package com.im;

import com.ip.TilePattern;
import mapgenerator.*;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 *
 * Created on Mar 03, 2009, 11:52:41 PM
 *
 * @author Sajarwo Anggai
 */
public class IMPanelWidgetPreview extends JPanel implements MouseListener {

    private BufferedImage bi;
    private TilePattern tp;
    private File location;
    private long fileSize;
    private Main comp;

    /**
     * Menampilkan image secara detail pada Image Manager Preview
     * @param render - object rendering
     * @param comp - Main Program
     * @param index - posisi image pada Image Container
     */
    public IMPanelWidgetPreview(Main comp, String loc) {
        this.comp = comp;
        this.location = new File(loc);;
        setToolTipText(location.getName());
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        addMouseListener(this);      
        fileSize = location.getUsableSpace();
        try {
            bi = ImageIO.read(location);
            tp = new TilePattern("",bi,0);
        } catch (IOException ex) {
        }
    }

    public void paint(Graphics g) {
        super.paintComponent(g);
        g.drawImage(IMControlPanel.imgBackground, 0, 0, 173, 100, null);
        g.drawImage(bi, 10, 10, 153, 80, null);
    }

    public void mouseClicked(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            comp.imControlPanel.setImageManagerInfo(bi.getWidth(), bi.getHeight(), fileSize);
            comp.imControlPanel.setImageContainerActive(location.getName());
            comp.ImageManagerRightPanelRender.removeAll();
            comp.ImageManagerRightPanelRender.add(tp).setBounds(10, 10, bi.getWidth(), bi.getHeight());
            comp.ImageManagerRightPanelRender.setPreferredSize(new Dimension(bi.getWidth() + 20, bi.getHeight() + 20));
            comp.ImageManagerRightPanelRender.repaint();
            comp.ImageManagerRightPanelRender.revalidate();
            comp.imControlPanel.showImgeManagerInfo(true);
        }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        setBackground(Color.GREEN);
    }

    public void mouseExited(MouseEvent e) {
        setBackground(null);
    }
}
